package com.hngy.xpq.android_practice.ui.notifications;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.Toast;

import com.hngy.xpq.android_practice.R;
import com.hngy.xpq.android_practice.adapter.CommAdapter;
import com.hngy.xpq.android_practice.bean.Comm;
import com.hngy.xpq.android_practice.bean.Recording;
import com.hngy.xpq.android_practice.bean.Wallet;
import com.hngy.xpq.android_practice.database.SQLiteComm;
import com.hngy.xpq.android_practice.database.SQLiteRecording;
import com.hngy.xpq.android_practice.ui.home.User_Comm;

import java.util.ArrayList;
import java.util.List;

public class RecordingComm extends AppCompatActivity {

    GridView listView;
    CommAdapter adapter;
    Comm c;
    List<Recording> list;
    List<Comm> lists,listing;
    SharedPreferences main,comm;
    SQLiteRecording sqLiteRecording;
    SQLiteComm sqLiteComm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.comm_recording);

        listView = findViewById(R.id.comm_recording_list);
        //调用本地存储，获得用户名
        main = super.getSharedPreferences("main",MODE_PRIVATE);
        comm = super.getSharedPreferences("comm",MODE_PRIVATE);
        RecordingDate();

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(RecordingComm.this, User_Comm.class);
                Comm c = listing.get(position);
                SharedPreferences.Editor editor = comm.edit();
                editor.putString("name",c.getMerchant_name());
                editor.putString("price",c.getComm_price());
                editor.putString("phone",c.getComm_phone());
                editor.putString("comm_name",c.getComm_name());
                editor.putString("position", String.valueOf(position));
                editor.putString("math","");
                editor.putString("status",c.getStatus());
                editor.commit();
                startActivity(intent);
            }
        });
    }

    private void RecordingDate() {
        sqLiteRecording = new SQLiteRecording(this);
        sqLiteComm = new SQLiteComm(this);
        showQueryRecording();
    }

    private void showQueryRecording() {
        if (list != null){
            list.clear();
        }
        if (comm.getString("like","").equals("recording")){
            list = sqLiteRecording.recordingQuery(main.getString("name",""));
        } else {
            list = sqLiteRecording.recordingLikeQuery("like");
        }
        listing = new ArrayList<Comm>();
//        Recording r = list.get(0);
//        Toast.makeText(RecordingComm.this,r.getRecording_name(),Toast.LENGTH_SHORT).show();
        for (int i = 0 ; i<list.size() ; i++){
            Recording r = list.get(i);
            //获得商品名
            String name = r.getRecording_name();
            //重新组建list
            if (lists != null){
                lists.clear();
            }
            lists = sqLiteComm.CommSearch(name);
            c = new Comm();
            c = lists.get(0);
            //组建后
            listing.add(c);
        }
        adapter = new CommAdapter(this,listing);
        listView.setAdapter(adapter);
    }
}